import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '@/views/Login/Login.vue'
// import Reg from '@/views/Reg/Reg.vue'
// import Main from '@/views/Main/Main.vue'
import store from '../store'
// // 1. 导入 Home 组件
// import Home from '@/views/Menu/Home/Home.vue'
// import Userinfo from '@/views/Menu/user/userinfo.vue'
// import UserAvatar from '@/views/Menu/user/userAvatar.vue'
// import UserPwd from '@/views/Menu/user/UserPwd.vue'
// import ArtCate from '@/views/Menu/article/ArtCate.vue'
// import ArtList from '@/views/Menu/article/ArtList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/Login.vue')
  },
  { path: '/reg', name: 'Reg', component: () => import('@/views/Reg/Reg.vue') },
  {
    path: '/',
    name: 'Main',
    component: () => import('@/views/Main/Main.vue'),
    // 3. 开启路由重定向
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/Menu/Home/Home.vue')
      },
      {
        path: '/user-info',
        name: 'User',
        component: () => import('@/views/Menu/user/userinfo.vue')
      },
      {
        path: '/user-avatar',
        name: 'Useravatar',
        component: () => import('@/views/Menu/user/userAvatar.vue')
      },
      {
        path: '/user-pwd',
        name: 'UserPwd',
        component: () => import('@/views/Menu/user/UserPwd.vue')
      },
      {
        path: 'art-cate',
        name: 'ArtCate',
        component: () => import('@/views/Menu/article/ArtCate.vue')
      },
      {
        path: 'art-list',
        name: 'ArtList',
        component: () => import('@/views/Menu/article/ArtList.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由前置导航守卫，路由切换之前进行拦截
// 参数1：到哪里去W
// 参数2：从哪里来
// 参数3：是否放行
router.beforeEach((to, from, next) => {
  if (to.path === '/' && !store.state.token) {
    Vue.prototype.$message.error('别搞事')
    next('/login')
  }
  next()
})
export default router
